package com.erps.CommonUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erps.db.DBManager;

public class DateUtils {
	private static final Logger log = LoggerFactory.getLogger(DateUtils.class);

	public static String getDayTime() {
		return DBManager.ServerDateTime();
	}

	public static String getDay() {
		return getDayTime().substring(0, 10);
	}

	public static String getYear() {
		return DBManager.ServerDateTime().substring(0, 4);
	}

	public static String getDayNum() {
		return getDay().replace("-", "");
	}

	public static String getDayNum(String cDay) {
		return cDay.replace("-", "");
	}

	public static String getDayTimeNum(String cDateTime) {
		SimpleDateFormat inputSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat outputSDF = new SimpleDateFormat("yyyyMMddHHmmss");
		Date vDate = null;
		try {
			vDate = inputSDF.parse(String.valueOf(cDateTime));
			return outputSDF.format(vDate);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Long getDayTimeNum() {
		String cDayTime = DBManager.ServerDateTime();
		SimpleDateFormat inputSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat outputSDF = new SimpleDateFormat("yyyyMMddHHmmss");
		Date vDate = null;
		try {
			vDate = inputSDF.parse(String.valueOf(cDayTime));
			return Long.parseLong(outputSDF.format(vDate));
		} catch (ParseException e) {
			return null;
		}
	}

	public static String getDayMin(String cDay) {
		String cStr = cDay.replace(":", "").replace("-", "").replace(" ", "");
		return cStr.substring(0, 8) + "000000";
	}

	public static String getDayMax(String cDay) {
		String cStr = cDay.replace(":", "").replace("-", "").replace(" ", "");
		return cStr.substring(0, 8) + "999999";
	}

	public static String getDayAdd(String cDay, int iDay) {
		Date nowDate = getDateValue(cDay, "yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(nowDate);
		c.add(Calendar.DAY_OF_YEAR, iDay);
		Date vDate = c.getTime();
		String vDay = getDateStr(vDate, "yyyy-MM-dd");
		return vDay;
	}

	public static String getDateStr(Date vDate, String cFormat) {
		if (vDate == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(cFormat);
		String cStr = "";
		try {
			cStr = sdf.format(vDate);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return cStr;
	}

	public static String getDateVal(String lTime) {
		SimpleDateFormat inputSDF = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat outputSDF = new SimpleDateFormat("yyyy-MM-dd");
		Date vDate = null;
		try {
			vDate = inputSDF.parse(String.valueOf(lTime));
			return outputSDF.format(vDate);
		} catch (ParseException e) {
			return null;
		}
	}

	public static String getDateTimeVal(String lTime) {
		SimpleDateFormat inputSDF = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat outputSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date vDate = null;
		try {
			vDate = inputSDF.parse(String.valueOf(lTime));
			return outputSDF.format(vDate);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date getDateTimeVal(Long lTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date vDate = null;
		try {
			vDate = sdf.parse(String.valueOf(lTime));
		} catch (ParseException e) {
			;
		}
		return vDate;
	}

	public static Date getDateValue(Long lTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date vDate = null;
		try {
			vDate = sdf.parse(String.valueOf(lTime));
		} catch (ParseException e) {
			;
		}
		return vDate;
	}

	public static Date getDateValue(String cDate, String cFormat) {
		SimpleDateFormat sdf = new SimpleDateFormat(cFormat);
		Date vDate = null;
		try {
			vDate = sdf.parse(cDate);
		} catch (ParseException e) {
			;
		}
		return vDate;
	}

	public static long StrToDateTime(String cFieldValue) {
		return Date.parse(cFieldValue);
	}
}
